package cn.edu.sdjzu.xg.eagle.bysj.controller;

import cn.edu.sdjzu.xg.eagle.bysj.annotation.IgnoreAuth;
import cn.edu.sdjzu.xg.eagle.bysj.annotation.SysLog;
import cn.edu.sdjzu.xg.eagle.bysj.entity.KaoqinshensuEntity;
import cn.edu.sdjzu.xg.eagle.bysj.entity.view.KaoqinshensuView;
import cn.edu.sdjzu.xg.eagle.bysj.service.Interface.KaoqinshensuService;
import cn.edu.sdjzu.xg.eagle.bysj.utils.MPUtil;
import cn.edu.sdjzu.xg.eagle.bysj.utils.PageUtils;
import cn.edu.sdjzu.xg.eagle.bysj.utils.R;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.*;

/**
 * 考勤申诉
 * 后端接口
 * @author 
 * @email 
 * @date 2025-05-27 14:26:11
 */
@RestController
@RequestMapping("/kaoqinshensu")
public class KaoqinshensuController {
    @Autowired
    private KaoqinshensuService kaoqinshensuService;









    /**
     * 后台列表
     */
    @RequestMapping("/page")
    public R page(@RequestParam Map<String, Object> params, KaoqinshensuEntity kaoqinshensu,
                  @RequestParam(required = false) @DateTimeFormat(pattern="yyyy-MM-dd HH:mm:ss") Date shensushijianStart,
                  @RequestParam(required = false) @DateTimeFormat(pattern="yyyy-MM-dd HH:mm:ss") Date shensushijianEnd,
                  HttpServletRequest request){
                    String tableName = request.getSession().getAttribute("tableName").toString();
                                            if(tableName.equals("yuangong")) {
                    kaoqinshensu.setYuangonggonghao((String)request.getSession().getAttribute("username"));
                                    }
                                                                                                                                                                                                                                                                    EntityWrapper<KaoqinshensuEntity> ew = new EntityWrapper<KaoqinshensuEntity>();
                                                                                                                                                                                        if(shensushijianStart!=null) ew.ge("shensushijian", shensushijianStart);
                    if(shensushijianEnd!=null) ew.le("shensushijian", shensushijianEnd);
                                                                            
        
        
        PageUtils page = kaoqinshensuService.queryPage(params, MPUtil.sort(MPUtil.between(MPUtil.likeOrEq(ew, kaoqinshensu), params), params));
        return R.ok().put("data", page);
    }







    /**
     * 前端列表
     */
	@IgnoreAuth
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params,KaoqinshensuEntity kaoqinshensu,
                @RequestParam(required = false) @DateTimeFormat(pattern="yyyy-MM-dd HH:mm:ss") Date shensushijianStart,
                @RequestParam(required = false) @DateTimeFormat(pattern="yyyy-MM-dd HH:mm:ss") Date shensushijianEnd,
		HttpServletRequest request){
        EntityWrapper<KaoqinshensuEntity> ew = new EntityWrapper<KaoqinshensuEntity>();
                if(shensushijianStart!=null) ew.ge("shensushijian", shensushijianStart);
                if(shensushijianEnd!=null) ew.le("shensushijian", shensushijianEnd);

		PageUtils page = kaoqinshensuService.queryPage(params, MPUtil.sort(MPUtil.between(MPUtil.likeOrEq(ew, kaoqinshensu), params), params));
        return R.ok().put("data", page);
    }

	/**
     * 列表
     */
    @RequestMapping("/lists")
    public R list( KaoqinshensuEntity kaoqinshensu){
       	EntityWrapper<KaoqinshensuEntity> ew = new EntityWrapper<KaoqinshensuEntity>();
      	ew.allEq(MPUtil.allEQMapPre( kaoqinshensu, MPUtil.camelToSnake("kaoqinshensu")));
        return R.ok().put("data", kaoqinshensuService.selectListView(ew));
    }

	 /**
     * 查询
     */
    @RequestMapping("/query")
    public R query(KaoqinshensuEntity kaoqinshensu){
        EntityWrapper< KaoqinshensuEntity> ew = new EntityWrapper< KaoqinshensuEntity>();
 		ew.allEq(MPUtil.allEQMapPre( kaoqinshensu, MPUtil.camelToSnake("kaoqinshensu")));
		KaoqinshensuView kaoqinshensuView =  kaoqinshensuService.selectView(ew);
		return R.ok("查询考勤申诉成功").put("data", kaoqinshensuView);
    }
	
    /**
     * 后端详情
     */
    @RequestMapping("/info/{id}")
    public R info(@PathVariable("id") Long id){
        KaoqinshensuEntity kaoqinshensu = kaoqinshensuService.selectById(id);
		kaoqinshensu = kaoqinshensuService.selectView(new EntityWrapper<KaoqinshensuEntity>().eq("id", id));
        return R.ok().put("data", kaoqinshensu);
    }

    /**
     * 前端详情
     */
	@IgnoreAuth
    @RequestMapping("/detail/{id}")
    public R detail(@PathVariable("id") Long id){
        KaoqinshensuEntity kaoqinshensu = kaoqinshensuService.selectById(id);
		kaoqinshensu = kaoqinshensuService.selectView(new EntityWrapper<KaoqinshensuEntity>().eq("id", id));
        return R.ok().put("data", kaoqinshensu);
    }
    



    /**
     * 后端保存
     */
    @RequestMapping("/save")
    @SysLog("新增考勤申诉")
    public R save(@RequestBody KaoqinshensuEntity kaoqinshensu, HttpServletRequest request){
    	//ValidatorUtils.validateEntity(kaoqinshensu);
        kaoqinshensuService.insert(kaoqinshensu);
        return R.ok();
    }
    
    /**
     * 前端保存
     */
    @SysLog("新增考勤申诉")
    @RequestMapping("/add")
    public R add(@RequestBody KaoqinshensuEntity kaoqinshensu, HttpServletRequest request){
    	//ValidatorUtils.validateEntity(kaoqinshensu);
        kaoqinshensuService.insert(kaoqinshensu);
        return R.ok();
    }



    /**
     * 修改
     */
    @RequestMapping("/update")
    @Transactional
    @SysLog("修改考勤申诉")
    public R update(@RequestBody KaoqinshensuEntity kaoqinshensu, HttpServletRequest request){
        //ValidatorUtils.validateEntity(kaoqinshensu);
        kaoqinshensuService.updateById(kaoqinshensu);//全部更新
        return R.ok();
    }

    @RequestMapping("/shBatch")
    @Transactional
    @SysLog("审核考勤申诉")
    public R update(@RequestBody Long[] ids, @RequestParam String sfsh, @RequestParam String shhf){
        List<KaoqinshensuEntity> list = new ArrayList<KaoqinshensuEntity>();
        for(Long id : ids) {
            KaoqinshensuEntity kaoqinshensu = kaoqinshensuService.selectById(id);
            kaoqinshensu.setSfsh(sfsh);
            kaoqinshensu.setShhf(shhf);
            list.add(kaoqinshensu);
        }
        kaoqinshensuService.updateBatchById(list);
        return R.ok();
    }


    

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @SysLog("删除考勤申诉")
    public R delete(@RequestBody Long[] ids){
        kaoqinshensuService.deleteBatchIds(Arrays.asList(ids));
        return R.ok();
    }

    	









}
